﻿using AutoMapper;
using B.S.DataBase.SuYuan.Domains.Entites;
using B.S.DataBase.SuYuan.ErrorCode;
using B.S.DataBase.SuYuan.Infrastructure.Interfaces;
using B.S.DataBase.SuYuan.Write.API.Applications.Commands.ShopInfo;
using MediatR;

namespace B.S.DataBase.SuYuan.Write.API.Applications.CommandHandlers.ShopInfo
{
    public class ShopCreateCommandHandler : IRequestHandler<ShopCreateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<ShopInfoModel> sRepo;
        private readonly IMapper mapper;

        public ShopCreateCommandHandler(IBaseRepository<ShopInfoModel> sRepo, IMapper mapper)
        {
            this.sRepo = sRepo;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(ShopCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = mapper.Map<ShopInfoModel>(request);
            model.CreateTime = DateTime.Now;
            var r = sRepo.Add(model).Result;
            if (r > 0)
            {
                result.Code = ResultCode.添加成功;
                result.Msg = ResultCode.添加成功.ToString();
                result.Data = r;
            }
            else
            {
                result.Code = ResultCode.添加失败;
                result.Msg = ResultCode.添加失败.ToString();
            }

            return await Task.FromResult(result);


        }



    }
}
